package com.study.controller;

import com.github.pagehelper.PageInfo;
import com.study.entity.AppCategory;
import com.study.entity.AppInfo;
import com.study.entity.User;
import com.study.service.AppInforService;
import com.study.service.AppcategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/18.
 */
@Controller
@RequestMapping("app")
public class AppController {
//    @RequestMapping("/version/list")
//    public void app(Model model){
//        model.addAttribute("username",new User());
//
//    }
    @Autowired
    private AppInforService appInforService;
    @Autowired
    private AppcategoryService appcategoryService;
    @RequestMapping("list")
    public String list(Model model, AppInfo appInfo,
                       @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "size",required = false,defaultValue = "6")Integer size){
        List<AppInfo> list = appInforService.findList(appInfo,page,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "app/list";
    }

    @RequestMapping("add")
    public void add(){}


    @RequestMapping("getLevel2")//二级下拉列表，联动
    @ResponseBody
    public List<AppCategory>  getLevel2( Integer pid){
        List<AppCategory> data = appcategoryService.getLevel2(pid);
         return  data;
    }
    @RequestMapping("doAdd")
    public String doAdd(Model model, AppInfo appInfo,@RequestParam(name="img",required = false) MultipartFile multipartFile) throws IOException {
        //    TODO判断文件属性
        String filePath=System.currentTimeMillis()+multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File("E:/",filePath));
        appInfo.setImgUrl(filePath);
        appInforService.addAppInfo(appInfo);
        return "redirect:list";
    }

//    @RequestMapping("add")
//    public void add(AppInfo appInfo){
//        appInforService.addAppInfo(appInfo);
//    }
}
